package com.szh.mysobproject.ui.activity

import android.annotation.SuppressLint
import android.view.View
import androidx.activity.viewModels
import androidx.collection.SparseArrayCompat
import androidx.collection.set
import by.kirich1409.viewbindingdelegate.viewBinding
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.badge.BadgeUtils
import com.hjq.bar.TitleBar
import com.szh.mysobproject.R
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.databinding.MessageCenterActivityBinding
import com.szh.mysobproject.model.msg.UnReadMsgCount
import com.szh.mysobproject.ui.activity.msg.*
import com.szh.mysobproject.util.createDefaultStyleBadge
import com.szh.mysobproject.util.setFixOnClickListener
import com.szh.mysobproject.util.startActivity
import com.szh.mysobproject.util.takeIfLogin
import com.szh.mysobproject.viewmodel.MsgViewModel

class MessageCenterActivity : AppActivity() {

    private val mBinding by viewBinding<MessageCenterActivityBinding>()
    private val mMsgViewModel by viewModels<MsgViewModel>()
    private val mDrawableCacheMap = SparseArrayCompat<BadgeDrawable>(4)

    override fun getLayoutId(): Int = R.layout.message_center_activity

    override fun initView() {
        //隐藏状态栏
        supportActionBar?.hide()
    }

    override fun initData() {

    }

    override fun initEvent() {
        //文章评论列表信息
        mBinding.articleContainer.setFixOnClickListener {

        }
        //点赞消息列表
        mBinding.likeContainer.setFixOnClickListener {
            takeIfLogin {
                startActivity<LikeMsgListActivity>()
            }
        }
        //摸鱼评论列表消息
        mBinding.fishContainer.setFixOnClickListener {
            takeIfLogin {
                startActivity<FishMsgListActivity>()
            }
        }
        //@我的消息
        mBinding.atMeContainer.setFixOnClickListener {
            startActivity<AtMeMsgListActivity>()
        }
        //问答消息
        mBinding.qaContainer.setFixOnClickListener {
            startActivity<QaMsgListActivity>()
        }
        //系统消息
        mBinding.systemContainer.setFixOnClickListener {
            startActivity<SystemMsgListActivity>()
        }
    }

    override fun onResume() {
        super.onResume()
        mMsgViewModel.getUnReadMsgCount().observe(this) {
            val unReadMsgCount = it.getOrNull() ?: return@observe
            setUnReadCountByMenu(unReadMsgCount)
        }
    }

    private fun setUnReadCountByMenu(unReadMsgCount: UnReadMsgCount) {
        mBinding.ivArticle.setUnReadCount(unReadMsgCount.articleMsgCount)
        mBinding.ivLike.setUnReadCount(unReadMsgCount.thumbUpMsgCount)
        mBinding.ivFish.setUnReadCount(unReadMsgCount.momentCommentCount)
        mBinding.ivAtMe.setUnReadCount(unReadMsgCount.atMsgCount)
        mBinding.ivQa.setUnReadCount(unReadMsgCount.wendaMsgCount)
        mBinding.ivSystem.setUnReadCount(unReadMsgCount.systemMsgCount)
    }

    @SuppressLint("UnsafeOptInUsageError")
    private fun View.setUnReadCount(unReadCount: Int) {
        val anchor = this
        val viewId = id
        //通过view 的 id 从缓存中获取BadgeDrawable
        val drawable = mDrawableCacheMap[viewId]
        if (drawable == null) {
            //如果缓存里没有 则生成
            createDefaultStyleBadge(context, unReadCount).apply {
                BadgeUtils.attachBadgeDrawable(this, anchor)
                mDrawableCacheMap[viewId] = this
            }
        } else {
            //否则更新 badgeDrawable 的状态
            drawable.isVisible = unReadCount > 0
            drawable.number = unReadCount
        }
    }

    override fun onRightClick(titleBar: TitleBar?) {
        takeIfLogin {
            //现在UI上响应客户 再请求网络
            setUnReadCountByMenu(UnReadMsgCount())
            mMsgViewModel.readAllMsg().observe(this) {
                val tips = it.getOrNull() ?: return@observe
                toast(tips)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        // BadgeDrawable 内部是弱引用持有 View，我们不关心 View 的释放问题
        mDrawableCacheMap.clear()
    }
}